package com.tfswue.zqm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tfswue.zqm.entity.User;
import com.tfswue.zqm.service.Userservice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/users")
public class RestFulUserController {
    @Autowired
    private Userservice userservice;

    //查询所有用户
    @GetMapping
    public List<User> getAllUsers() {
        return userservice.list();
    }

    //查询单个用，根据id
    @GetMapping("/{id}/{name}")
    public User getUserById(@PathVariable("id") Integer id, @PathVariable("name") String name) {
        log.error("id:{},name:{}", id, name);
        return userservice.getById(id);
    }
//模糊查询
    @GetMapping("/querylist/{page}/{size}")
    public List<User> queryList(@PathVariable("page") Integer page,
                                @PathVariable("size") Integer size,
                                @RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (user.getUsername() != null) {
            queryWrapper.like("username", user.getUsername());
        }
        if (user.getEmail() != null) {
            queryWrapper.like("email", user.getEmail());
        }
        Page<User> testpage = new Page<>(page, size);
        Page<User> userPage = userservice.page(testpage, queryWrapper);
        return userPage.getRecords();
    }
    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return userservice.save(user);
    }
}